//
//  SCXCreateAlertView.m
//  自定义弹出视图
//
//  Created by Michael on 15/12/31.
//  Copyright © 2015年 Michael. All rights reserved.
//

#import "SCXCreateAlertView2.h"
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#define ALERTWIDTH 150
#define ALERTHEIGHT 50
#define BUTTONALERTVIEWWIDTH 280
#define BUTTONALERTVIEWHEIGHT (180)
#define CRACKWIDTH 50
#define COLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define HEIGHT [UIScreen mainScreen].bounds.size.height
static SCXCreateAlertView2 *singCreateAlertView =nil;
@implementation SCXCreateAlertView2
#pragma mark--单例
+(SCXCreateAlertView2 *)shareSingleCreateAlertView{

    static dispatch_once_t once;
    dispatch_once(&once, ^{
        singCreateAlertView=[[SCXCreateAlertView2 allocWithZone:NULL]init];
    });
    return singCreateAlertView;
}
#pragma mark--自定义下拉菜单
-(instancetype)initWithNameArray:(NSMutableArray *)nameArray andMenuOrigin:(CGPoint)orign andMenuWidth:(CGFloat)width andHeight:(CGFloat)rowHeight andLayer:(CGFloat)layer andTableViewBackGroundColor:(UIColor *)color andIsSharp:(BOOL)sharp andType:(popType2)poptype{
    self.view=[super initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) ];
    if (self.view) {
        
        _popViewType=poptype;
        _isSharp=sharp;
        _layerSize=layer;
        _width=width;
        _orign=orign;
        _nameArray=nameArray;
        _color=color;
        self.backgroundColor=[UIColor clearColor];
        if (nameArray.count < 6) {
            self.menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(orign.x, orign.y,_width, rowHeight *_nameArray.count) style:UITableViewStylePlain];
        }else{
            self.menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(orign.x, orign.y,_width, rowHeight *4) style:UITableViewStylePlain];
        }

        [self.menuTableView setBackgroundColor:color];
        [self.menuTableView setDelegate:self];
        [self.menuTableView setDataSource:self];
       // self.menuTableView.rowHeight=rowHeight;
        [self.menuTableView.layer setMasksToBounds:YES];
        [self.menuTableView.layer setCornerRadius:layer];
        self.menuTableView.layer.borderWidth = 1;
        self.menuTableView.layer.borderColor = [[UIColor grayColor] CGColor];
        [self.menuTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"downMenuCell"];
        //设置下拉菜单边框颜色，如果不需要可以注掉 。
         [self.menuTableView   setSeparatorColor:[UIColor lightGrayColor]];
        if ([self.menuTableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.menuTableView setSeparatorInset: UIEdgeInsetsZero];
        }
        if ([self.menuTableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.menuTableView setLayoutMargins: UIEdgeInsetsZero];
        }
      [self.view addSubview:_menuTableView];
    }
    return self;
}
#pragma mark--tableView代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"downMenuCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"downMenuCell"] ;
    }
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0.12,0.61,0.89,1});
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.text = _nameArray[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
    cell.accessoryType  = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell.highlighted) {
        cell.backgroundColor = [UIColor colorWithCGColor:color];
    }
    
    return cell;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _nameArray.count;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//    if ([self.tableViewDelegate respondsToSelector:@selector(secondtableViewDidSelectRowAtIndexPath: andPopType:)]) {
        [self.tableViewDelegate secondtableViewDidSelectRowAtIndexPath:indexPath andPopType:_popViewType];
 //   }
}

-(void)layoutSubviews{
    [super layoutSubviews];

}

#pragma mark--自定义背景虚拟效果,读者根据自己的喜好自定义。
-(UIView *)createBackGroundView{
    self.frame=[UIScreen mainScreen].bounds ;
    self.backgroundColor=[UIColor blackColor];
    //self.backgroundColor=COLOR(121, 121, 121);
    self.alpha=0.7;
    _view=self;
    return _view;

}
#pragma mark--消失事件
-(void)dismissWithCompletion:(void (^)(SCXCreateAlertView2 *))completion{
    __weak typeof(self)weakSelf=self;
    [UIView animateWithDuration:0.1 animations:^{
        weakSelf.menuTableView.frame = CGRectMake(_orign.x, _orign.y,_width, 0);
            } completion:^(BOOL finished) {
                
        if (weakSelf.dismiss) {
            weakSelf.dismiss();
        }
    }];
}
-(void)animation{
    if (self.dismiss) {
        self.dismiss();
    }
}
#pragma mark--touch取消事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_textField resignFirstResponder];
    [self dismissWithCompletion:nil];

}

 
@end
